package com.ole.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.Converter;

public class UtilDateConvert implements Converter {
	private String[] dateFormatStrings;
	private final static String defaultFormat = "yyyy-MM-dd";

	public String[] getDateFormatStrings() {
		return dateFormatStrings;
	}

	public void setDateFormatStrings(String[] dateFormatStrings) {
		this.dateFormatStrings = dateFormatStrings;
	}

	@SuppressWarnings("unchecked")
	public Object convert(Class arg0, Object arg1) {
		if(arg1==null||arg1.toString().trim().length()==0) return null;
		SimpleDateFormat sdf = null;
		if(this.getDateFormatStrings().length>0){
		for(String s : this.getDateFormatStrings()){
			sdf = new SimpleDateFormat(s);
			try {
				return sdf.parse(arg1.toString().trim());
			} catch (ParseException e) {
			}
		}
		}else{
			sdf=new SimpleDateFormat(defaultFormat);
			try {
				return sdf.parse(arg1.toString());
			} catch (ParseException e) {
				return null;
			}
		}
		return null;
	}

}
